package com.digiregistry.api.util;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class AuthDigester {

	private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
	private Mac mac;

	public AuthDigester(String key) {
		try {
			byte[] bytes = key.getBytes("UTF-8");
			SecretKeySpec signingKey = new SecretKeySpec(bytes, HMAC_SHA1_ALGORITHM);
			mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
			mac.init(signingKey);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	public void update(String token) {
		try {
			mac.update(token.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException ex) {
			throw new RuntimeException(ex);
		}
	}

	public String digest() {
		return Digest.toHex(mac.doFinal());
	}

}
